<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Question extends Model
{
    /*添加问题API*/
    public function add(){

        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }
        $title = rq('title');
        $desc = rq('desc');
        if (!$title) return ['status'=>0,'msg'=>'title require'];
        if (!$desc) return ['status'=>0,'msg'=>'desc require'];

        //自动执行的事务方式
//        DB::beginTransaction(function ($title,$desc){
//            $this->user_id = session('user_id');
//            $this->title = $title;
//            $this->desc = $desc;
//            $this->save();
//            $data = ['user_id'=>session('user_id'),'question_id'=>$this->id]
//            DB::table('question_user')->insert($data);
//        });
        DB::beginTransaction();

        $this->user_id = session('user_id');
        $this->title = $title;
        $this->desc = $desc;
        $r = $this->save();

        if (!$r){
            DB::rollback();
            return err('db error');
        }
        $data = ['user_id'=>session('user_id'),'question_id'=>$this->id];
        $r = DB::table('question_user')->insert($data);


        if (!$r){
            DB::rollback();
            return err('db error');
        }
        DB::commit();
        return suc('success');

    }
    /*修改问题API*/
    public function change(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }
        if (rq('id')) return ['status'=>0,'msg'=>'id require'];
        $question = $this->find(rq('id'));
        if (!$question) ['status'=>0,'msg'=>'question not exists'];
        if ($question.user_id != sesssion('user_id')) {
            return ['status'=>0,'msg'=>'permission denied'];
        }
        if (rq('title')){
            $question->title = rq('title');
        }
        if (rq('desc')){
            $question->desc = rq('desc');
        }

        return $this->save()?['status'=>1]:['status'=>0,'db update failed'];
    }
    /*查看问题API*/
    public function read(){
        if (rq('id')){
            $data = $this->find(rq('id'));
            return ['status'=>1,'msg'=>'success','data'=>$data];
        }

        list($limit,$skip) = paginate(rq('page'),rq('limit'));

        $data =  $this->orderBy('created_at')
            ->limit($limit)
            ->skip($skip)
            ->get(['id','title','desc','user_id','created_at','updated_at'])
            ->keyBy('id');

        return ['status'=>1,'msg'=>'success','data'=>$data];

    }

    /*删除问题API*/
    public function remove(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }

        if (!rq('id')){
            return ['status'=>0,'msg'=>'id require'];
        }
        $question = $this->find(rq('id'));
        if (!$question) {
            return ['status'=>0,'msg'=>'question not exists'];
        }
        if ($question->user_id != session('user_id')){
            return ['status'=>0,'msg'=>'permission denied'];
        }
        $question->delete()?['status'=>1,'msg'=>'delete success']:['status'=>0,'msg'=>'db delete failed'];

    }
    public function user(){
        return $this->belongsTo('App\User');
    }
}
